package com.jianyun.jyzs.fragment;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.FlowExamineActivity;
import com.jianyun.jyzs.activity.FlowExamineDetailActivity;
import com.jianyun.jyzs.activity.ShowFileActivity;
import com.jianyun.jyzs.base.MVPBaseFragment;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.presenter.ShareDoPresenter;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.NetUtil;
import com.jianyun.jyzs.view.iview.IShareView;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * 流程审批  共享任务
 * Created by yangshijun on 2018/5/29.
 */

public class ShareTaskFragment extends MVPBaseFragment<IShareView, ShareDoPresenter> implements IShareView {

    @BindView(R.id.nullNetWork)
    LinearLayout nullNetWork;
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    public WebSettings webSettings;
    public String[] data;
    private String userId;
    private FlowExamineActivity ac;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ac = (FlowExamineActivity) activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        data = new String[4];
        webSettings = webview.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(false);
        //放开storage缓存机制
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        searchData();
    }

    @Override
    protected int initContentLayout() {
        return R.layout.activity_app_workflow_item;
    }

    @Override
    public ShareDoPresenter createPresenter() {
        return new ShareDoPresenter();
    }

    /**
     * 	点击查询条件搜索后执行此方法
     */
    public void searchData() {
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        String weburl = erpRootUrl+ SysConstant.http_url_shareWorkFlowUrl.replaceAll("\\{employeeid\\}",userId);
        if(data != null){
            weburl += "&insName="+(data[0] == null?"":data[0]);
            weburl += "&taskName="+(data[1] == null?"":data[1]);
            weburl += "&sendEmployeeid="+(data[2] == null?"":ac.createUserId);
            weburl += "&sendTime="+(data[3] == null?"":data[3]);
        }
        //if (NetUtil.checkNetType(getActivity())) {
            //加载需要显示的网页
            webview.loadUrl(weburl);
            //设置Web视图
            webview.setWebViewClient(new webViewClient());

//        }else{
//            webview.setVisibility(View.GONE);
//            nullNetWork.setVisibility(View.VISIBLE);
//        }
    }
    //Web视图
    private class webViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url,Bitmap favicon) {//网页页面开始加载的时候
            super.onPageStarted(view, url,favicon);
            progressBar.setVisibility(View.VISIBLE);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
//	         //页面下载完毕,却不代表页面渲染完毕显示出来
//            if (webview.getContentHeight() != 0) { //这个时候网页才显示
            progressBar.setVisibility(View.INVISIBLE);
//            }
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // handle different requests for different type of files
            // this example handles downloads requests for .apk and .mp3 files
            // everything else the webview can handle normally
            //跳转界面
            Intent intent = new Intent(getActivity(), FlowExamineDetailActivity.class);
            intent.putExtra("url",url);
            startActivity(intent);

            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            //这里进行无网络或错误处理，具体可以根据errorCode的值进行判断，做跟详细的处理。
            view.loadUrl("file:///android_asset/error.html" );
        }
    }

}
